package com.yn.club.subject.application.controller;



import com.yn.club.subject.domain.entity.SubjectLikedDO;
import com.yn.club.subject.application.dto.SubjectLikedDTO;
import com.yn.club.subject.domain.service.SubjectLikedDomainService;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.NotEmpty;
import java.io.Serializable;
import java.util.List;
import com.yn.club.common.util.R;
import com.yn.club.common.util.PageReq;
import com.yn.club.common.util.PageResp;
import com.yn.club.subject.application.convert.SubjectLikedApplicationConverter;
import com.yn.club.subject.application.request.SubjectLikedQueryRequest;
import com.yn.club.subject.application.request.SubjectLikedSaveRequest;

/**
 * 题目点赞表(SubjectLiked)表控制层
 *
 * @author YN
 * @since 2025-01-21 17:40:47
 */
@Slf4j
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("subjectLiked")
public class SubjectLikedController {
    /**
     * 服务对象
     */
    private final SubjectLikedDomainService subjectLikedDomainService;
    private final SubjectLikedApplicationConverter subjectLikedApplicationConverter;

    /**
     * 分页查询数据
     *
     * @param pageReq 分页参数
     * @param query   查询条件
     * @return 所有数据
     */
    @GetMapping
    public R<PageResp<SubjectLikedDTO>> queryPage(PageReq pageReq, SubjectLikedQueryRequest query) {
        SubjectLikedDO subjectLikedDO = subjectLikedApplicationConverter.convertQueryRequestToDo(query);
        PageResp<SubjectLikedDO> page = subjectLikedDomainService.page(pageReq, subjectLikedDO);
        
        List<SubjectLikedDTO> subjectLikedDTOS = subjectLikedApplicationConverter.convertDoToDto(page.getList());
        return R.ok(PageResp.of(page,subjectLikedDTOS));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public R<SubjectLikedDTO> selectOne(@PathVariable Serializable id) {
    SubjectLikedDTO subjectLikedDTO = subjectLikedApplicationConverter.convertDoToDto(
      this.subjectLikedDomainService.getById(id));
     
    return R.ok(subjectLikedDTO);
    }

    /**
     * 新增数据
     *
     * @param subjectLiked 实体对象
     * @return 受影响条数
     */
    @PostMapping
    public R<Boolean> insert(@RequestBody SubjectLikedSaveRequest subjectLiked) {
       SubjectLikedDO subjectLikedDO = subjectLikedApplicationConverter.convertSaveRequestToDo(subjectLiked);
        return R.ok(this.subjectLikedDomainService.insert(subjectLikedDO));
    }

    /**
     * 修改数据
     *
     * @param subjectLiked 实体对象
     * @return 受影响条数
     */
    @PutMapping
    public R<Boolean> update(@RequestBody SubjectLikedSaveRequest subjectLiked) {
        SubjectLikedDO subjectLikedDO = subjectLikedApplicationConverter.convertSaveRequestToDo(subjectLiked);
        return R.ok(this.subjectLikedDomainService.updateById(subjectLikedDO));
    }

    /**
     * 删除数据
     *
     * @param ids 主键结合
     * @return 受影响条数
     */
    @DeleteMapping
    public R<Boolean> delete(@RequestBody @NotEmpty List<Integer> ids) {
        return R.ok(this.subjectLikedDomainService.delete(ids));
    }
}

